home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / wind2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  14.7 KB  |  462 lines

  1. /*
  2.  * Copyright 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <GL/glx.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <X11/keysym.h>
  23.  
  24. static int RGB_attributes_DB[] = {
  25.     GLX_RGBA,
  26.     GLX_RED_SIZE, 1,
  27.     GLX_GREEN_SIZE, 1,
  28.     GLX_BLUE_SIZE, 1,
  29.     GLX_DOUBLEBUFFER,
  30.     None,
  31. };
  32.  
  33. static int CI_attributes_DB[] = {
  34.     GLX_DOUBLEBUFFER,
  35.     None,
  36. };
  37.  
  38. static int RGB_attributes[] = {
  39.     GLX_RGBA,
  40.     GLX_RED_SIZE, 1,
  41.     GLX_GREEN_SIZE, 1,
  42.     GLX_BLUE_SIZE, 1,
  43.     None,
  44. };
  45.  
  46. static int CI_attributes[] = {
  47.     None,
  48. };
  49.  
  50. int rgb = 0;
  51. int W = 300;
  52. int H = 300;
  53. int doubleBuf = 1;
  54. GLXContext cx, cx2;
  55. Window window, window2;
  56. Display *dpy;
  57.  
  58. unsigned char font6x10[] = {
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  60. 0x00, 0x00, 0x10, 0x38, 0x38, 0x7c, 0x38, 0x38, 0x10, 0x00, 
  61. 0x00, 0x00, 0x54, 0x28, 0x54, 0x28, 0x54, 0x28, 0x54, 0x00, 
  62. 0x08, 0x08, 0x08, 0x3c, 0x48, 0x48, 0x78, 0x48, 0x48, 0x00, 
  63. 0x10, 0x10, 0x18, 0x10, 0x5c, 0x40, 0x60, 0x40, 0x70, 0x00, 
  64. 0x24, 0x24, 0x38, 0x24, 0x38, 0x38, 0x40, 0x40, 0x38, 0x00, 
  65. 0x20, 0x20, 0x38, 0x20, 0x3c, 0x78, 0x40, 0x40, 0x40, 0x00, 
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x28, 0x28, 0x38, 0x00, 
  67. 0x00, 0x00, 0x7c, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00, 0x00, 
  68. 0x3c, 0x20, 0x20, 0x20, 0x48, 0x58, 0x68, 0x68, 0x48, 0x00, 
  69. 0x08, 0x08, 0x08, 0x08, 0x3c, 0x20, 0x30, 0x48, 0x48, 0x00, 
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x10, 0x10, 0x10, 0x10, 
  71. 0x10, 0x10, 0x10, 0x10, 0x10, 0x70, 0x00, 0x00, 0x00, 0x00, 
  72. 0x10, 0x10, 0x10, 0x10, 0x10, 0x1c, 0x00, 0x00, 0x00, 0x00, 
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x10, 0x10, 0x10, 0x10, 
  74. 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x10, 0x10, 
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  78. 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  79. 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  80. 0x10, 0x10, 0x10, 0x10, 0x10, 0x1c, 0x10, 0x10, 0x10, 0x10, 
  81. 0x10, 0x10, 0x10, 0x10, 0x10, 0x70, 0x10, 0x10, 0x10, 0x10, 
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x10, 0x10, 0x10, 0x10, 
  83. 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  84. 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 
  85. 0x00, 0x7c, 0x04, 0x08, 0x20, 0x40, 0x20, 0x08, 0x04, 0x00, 
  86. 0x00, 0x7c, 0x40, 0x20, 0x08, 0x04, 0x08, 0x20, 0x40, 0x00, 
  87. 0x00, 0x00, 0x28, 0x28, 0x28, 0x28, 0x7c, 0x00, 0x00, 0x00, 
  88. 0x00, 0x00, 0x40, 0x20, 0x7c, 0x10, 0x7c, 0x08, 0x04, 0x00, 
  89. 0x00, 0x20, 0x6c, 0x38, 0x20, 0x78, 0x20, 0x24, 0x18, 0x00, 
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 
  91. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  92. 0x00, 0x00, 0x10, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 
  93. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x28, 0x28, 0x00, 
  94. 0x00, 0x00, 0x28, 0x28, 0x7c, 0x28, 0x7c, 0x28, 0x28, 0x00, 
  95. 0x00, 0x00, 0x10, 0x38, 0x14, 0x38, 0x50, 0x38, 0x10, 0x00, 
  96. 0x00, 0x00, 0x48, 0x54, 0x28, 0x10, 0x28, 0x54, 0x24, 0x00, 
  97. 0x00, 0x00, 0x34, 0x48, 0x54, 0x20, 0x50, 0x50, 0x20, 0x00, 
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x10, 0x18, 0x00, 
  99. 0x00, 0x00, 0x08, 0x10, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, 
  100. 0x00, 0x00, 0x20, 0x10, 0x08, 0x08, 0x08, 0x10, 0x20, 0x00, 
  101. 0x00, 0x00, 0x00, 0x44, 0x28, 0x7c, 0x28, 0x44, 0x00, 0x00, 
  102. 0x00, 0x00, 0x00, 0x10, 0x10, 0x7c, 0x10, 0x10, 0x00, 0x00, 
  103. 0x00, 0x20, 0x10, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 
  105. 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  106. 0x00, 0x00, 0x40, 0x40, 0x20, 0x10, 0x08, 0x04, 0x04, 0x00, 
  107. 0x00, 0x00, 0x10, 0x28, 0x44, 0x44, 0x44, 0x28, 0x10, 0x00, 
  108. 0x00, 0x00, 0x7c, 0x10, 0x10, 0x10, 0x50, 0x30, 0x10, 0x00, 
  109. 0x00, 0x00, 0x7c, 0x40, 0x20, 0x18, 0x04, 0x44, 0x38, 0x00, 
  110. 0x00, 0x00, 0x38, 0x44, 0x04, 0x18, 0x08, 0x04, 0x7c, 0x00, 
  111. 0x00, 0x00, 0x08, 0x08, 0x7c, 0x48, 0x28, 0x18, 0x08, 0x00, 
  112. 0x00, 0x00, 0x38, 0x44, 0x04, 0x64, 0x58, 0x40, 0x7c, 0x00, 
  113. 0x00, 0x00, 0x38, 0x44, 0x64, 0x58, 0x40, 0x20, 0x18, 0x00, 
  114. 0x00, 0x00, 0x20, 0x20, 0x10, 0x08, 0x08, 0x04, 0x7c, 0x00, 
  115. 0x00, 0x00, 0x38, 0x44, 0x44, 0x38, 0x44, 0x44, 0x38, 0x00, 
  116. 0x00, 0x00, 0x30, 0x08, 0x04, 0x34, 0x4c, 0x44, 0x38, 0x00, 
  117. 0x00, 0x10, 0x38, 0x10, 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 
  118. 0x00, 0x20, 0x10, 0x18, 0x00, 0x10, 0x38, 0x10, 0x00, 0x00, 
  119. 0x00, 0x00, 0x04, 0x08, 0x10, 0x20, 0x10, 0x08, 0x04, 0x00, 
  120. 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x00, 0x00, 
  121. 0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x10, 0x20, 0x40, 0x00, 
  122. 0x00, 0x00, 0x10, 0x00, 0x10, 0x10, 0x08, 0x44, 0x38, 0x00, 
  123. 0x00, 0x00, 0x38, 0x40, 0x58, 0x54, 0x4c, 0x44, 0x38, 0x00, 
  124. 0x00, 0x00, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x28, 0x10, 0x00, 
  125. 0x00, 0x00, 0x78, 0x24, 0x24, 0x38, 0x24, 0x24, 0x78, 0x00, 
  126. 0x00, 0x00, 0x38, 0x44, 0x40, 0x40, 0x40, 0x44, 0x38, 0x00, 
  127. 0x00, 0x00, 0x78, 0x24, 0x24, 0x24, 0x24, 0x24, 0x78, 0x00, 
  128. 0x00, 0x00, 0x7c, 0x40, 0x40, 0x78, 0x40, 0x40, 0x7c, 0x00, 
  129. 0x00, 0x00, 0x40, 0x40, 0x40, 0x78, 0x40, 0x40, 0x7c, 0x00, 
  130. 0x00, 0x00, 0x38, 0x44, 0x4c, 0x40, 0x40, 0x44, 0x38, 0x00, 
  131. 0x00, 0x00, 0x44, 0x44, 0x44, 0x7c, 0x44, 0x44, 0x44, 0x00, 
  132. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x10, 0x10, 0x38, 0x00, 
  133. 0x00, 0x00, 0x38, 0x44, 0x04, 0x04, 0x04, 0x04, 0x1c, 0x00, 
  134. 0x00, 0x00, 0x44, 0x48, 0x50, 0x60, 0x50, 0x48, 0x44, 0x00, 
  135. 0x00, 0x00, 0x7c, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 
  136. 0x00, 0x00, 0x44, 0x44, 0x44, 0x54, 0x6c, 0x44, 0x44, 0x00, 
  137. 0x00, 0x00, 0x44, 0x44, 0x4c, 0x54, 0x64, 0x44, 0x44, 0x00, 
  138. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00, 
  139. 0x00, 0x00, 0x40, 0x40, 0x40, 0x78, 0x44, 0x44, 0x78, 0x00, 
  140. 0x00, 0x04, 0x38, 0x54, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00, 
  141. 0x00, 0x00, 0x44, 0x48, 0x50, 0x78, 0x44, 0x44, 0x78, 0x00, 
  142. 0x00, 0x00, 0x38, 0x44, 0x04, 0x38, 0x40, 0x44, 0x38, 0x00, 
  143. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7c, 0x00, 
  144. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x00, 
  145. 0x00, 0x00, 0x10, 0x28, 0x28, 0x28, 0x44, 0x44, 0x44, 0x00, 
  146. 0x00, 0x00, 0x44, 0x6c, 0x54, 0x54, 0x44, 0x44, 0x44, 0x00, 
  147. 0x00, 0x00, 0x44, 0x44, 0x28, 0x10, 0x28, 0x44, 0x44, 0x00, 
  148. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x28, 0x44, 0x44, 0x00, 
  149. 0x00, 0x00, 0x7c, 0x40, 0x20, 0x10, 0x08, 0x04, 0x7c, 0x00, 
  150. 0x00, 0x00, 0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38, 0x00, 
  151. 0x00, 0x00, 0x04, 0x04, 0x08, 0x10, 0x20, 0x40, 0x40, 0x00, 
  152. 0x00, 0x00, 0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 
  153. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x28, 0x10, 0x00, 
  154. 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  155. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x30, 0x00, 
  156. 0x00, 0x00, 0x3c, 0x44, 0x3c, 0x04, 0x38, 0x00, 0x00, 0x00, 
  157. 0x00, 0x00, 0x58, 0x64, 0x44, 0x64, 0x58, 0x40, 0x40, 0x00, 
  158. 0x00, 0x00, 0x38, 0x44, 0x40, 0x44, 0x38, 0x00, 0x00, 0x00, 
  159. 0x00, 0x00, 0x34, 0x4c, 0x44, 0x4c, 0x34, 0x04, 0x04, 0x00, 
  160. 0x00, 0x00, 0x38, 0x40, 0x7c, 0x44, 0x38, 0x00, 0x00, 0x00, 
  161. 0x00, 0x00, 0x20, 0x20, 0x20, 0x78, 0x20, 0x24, 0x18, 0x00, 
  162. 0x38, 0x44, 0x38, 0x40, 0x30, 0x48, 0x34, 0x00, 0x00, 0x00, 
  163. 0x00, 0x00, 0x44, 0x44, 0x44, 0x64, 0x58, 0x40, 0x40, 0x00, 
  164. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x30, 0x00, 0x10, 0x00, 
  165. 0x30, 0x48, 0x48, 0x08, 0x08, 0x08, 0x18, 0x00, 0x08, 0x00, 
  166. 0x00, 0x00, 0x44, 0x48, 0x70, 0x48, 0x44, 0x40, 0x40, 0x00, 
  167. 0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x10, 0x10, 0x30, 0x00, 
  168. 0x00, 0x00, 0x44, 0x54, 0x54, 0x54, 0x68, 0x00, 0x00, 0x00, 
  169. 0x00, 0x00, 0x44, 0x44, 0x44, 0x64, 0x58, 0x00, 0x00, 0x00, 
  170. 0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00, 
  171. 0x40, 0x40, 0x40, 0x58, 0x64, 0x64, 0x58, 0x00, 0x00, 0x00, 
  172. 0x04, 0x04, 0x04, 0x34, 0x4c, 0x4c, 0x34, 0x00, 0x00, 0x00, 
  173. 0x00, 0x00, 0x40, 0x40, 0x40, 0x64, 0x58, 0x00, 0x00, 0x00, 
  174. 0x00, 0x00, 0x78, 0x04, 0x38, 0x40, 0x38, 0x00, 0x00, 0x00, 
  175. 0x00, 0x00, 0x18, 0x24, 0x20, 0x20, 0x78, 0x20, 0x20, 0x00, 
  176. 0x00, 0x00, 0x34, 0x4c, 0x44, 0x44, 0x44, 0x00, 0x00, 0x00, 
  177. 0x00, 0x00, 0x10, 0x28, 0x28, 0x44, 0x44, 0x00, 0x00, 0x00, 
  178. 0x00, 0x00, 0x28, 0x54, 0x54, 0x44, 0x44, 0x00, 0x00, 0x00, 
  179. 0x00, 0x00, 0x44, 0x28, 0x10, 0x28, 0x44, 0x00, 0x00, 0x00, 
  180. 0x38, 0x44, 0x04, 0x34, 0x4c, 0x44, 0x44, 0x00, 0x00, 0x00, 
  181. 0x00, 0x00, 0x7c, 0x20, 0x10, 0x08, 0x7c, 0x00, 0x00, 0x00, 
  182. 0x00, 0x00, 0x0c, 0x10, 0x08, 0x30, 0x08, 0x10, 0x0c, 0x00, 
  183. 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 
  184. 0x00, 0x00, 0x60, 0x10, 0x20, 0x18, 0x20, 0x10, 0x60, 0x00, 
  185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x54, 0x24, 0x00, 
  186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
  187. };
  188.  
  189. int fontbase = 256;
  190.  
  191. #define MAX_COLORS 15
  192.  
  193. float colors[MAX_COLORS][3] = {
  194.     { 1.0, 0.0, 0.0 },
  195.     { 0.8, 0.2, 0.0 },
  196.     { 0.6, 0.4, 0.0 },
  197.     { 0.4, 0.6, 0.0 },
  198.     { 0.2, 0.8, 0.0 },
  199.     { 0.0, 1.0, 0.0 },
  200.     { 0.0, 0.8, 0.2 },
  201.     { 0.0, 0.6, 0.4 },
  202.     { 0.0, 0.4, 0.6 },
  203.     { 0.0, 0.2, 0.8 },
  204.     { 0.0, 0.0, 1.0 },
  205.     { 0.2, 0.0, 0.8 },
  206.     { 0.4, 0.0, 0.6 },
  207.     { 0.6, 0.0, 0.4 },
  208.     { 0.8, 0.0, 0.2 },
  209. };
  210.  
  211. void initializeFont(void)
  212. {
  213.     int i;
  214.  
  215.     glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  216.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  217.  
  218.     for (i=0; i<128; i++) {
  219.     glNewList(i+fontbase, GL_COMPILE);
  220.     glBitmap(6, 10, 0.0, 0.0, 6.0, 0.0, font6x10+10*i);
  221.     glEndList();
  222.     }
  223. }
  224.  
  225. void writeFont(char *string)
  226. {
  227.     glListBase(fontbase);
  228.     glCallLists(strlen(string), GL_BYTE, (unsigned char *) string);
  229. }
  230.  
  231. static void Init(void)
  232. {
  233.     glViewport(0,0,W,H);
  234.     glMatrixMode(GL_PROJECTION);
  235.     glLoadIdentity();
  236.     glOrtho(0,W,0,H,-1,1);
  237.     glMatrixMode(GL_MODELVIEW);
  238.  
  239.     if (rgb) {
  240.     glClearColor(0,0,0,0);
  241.     } else {
  242.     glClearIndex(0);
  243.     }
  244.  
  245.     glClear(GL_COLOR_BUFFER_BIT);
  246.  
  247.     initializeFont();
  248. }
  249.  
  250. static void Redraw(void)
  251. {
  252.     int i, y;
  253.     static int color;
  254.  
  255.     color = (color+1) % MAX_COLORS;
  256.     for (i=0; i<2; i++) {
  257.     if (i == 0) {
  258.         glXMakeCurrent(dpy, window, cx);
  259.     } else {
  260.         glXMakeCurrent(dpy, window2, cx);
  261.     }
  262.  
  263.     if (rgb) {
  264.         glColor3fv(colors[color]);
  265.     } else {
  266.         glIndexi(color);
  267.     }
  268.  
  269.     glClear(GL_COLOR_BUFFER_BIT);
  270.  
  271.     glRasterPos2i(5, 50);
  272.     writeFont("Hello");
  273.     sginap(10);
  274.     glRasterPos2i(5, 40);
  275.     writeFont("There");
  276.     sginap(10);
  277.     glRasterPos2i(5, 30);
  278.     writeFont("World");
  279.  
  280.     if (doubleBuf) {
  281.         if (i == 0) {
  282.         glXSwapBuffers(dpy, window);
  283.         } else {
  284.         glXSwapBuffers(dpy, window2);
  285.         }
  286.     }
  287.     }
  288. }
  289.  
  290. static void Usage(void)
  291. {
  292.     fprintf(stderr, "Usage: wind2 [-c] [-s]\n");
  293.     fprintf(stderr, " -c  run in color index mode\n");
  294.     fprintf(stderr, " -s  run in singlebuffer mode\n");
  295.     exit(1);
  296. }
  297.  
  298. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  299. {
  300.     if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
  301.     return GL_TRUE;
  302.     }
  303.     return GL_FALSE;
  304. }
  305.  
  306. int main(int argc, char** argv)
  307. {
  308.     XVisualInfo *vi;
  309.     Colormap cmap;
  310.     XSetWindowAttributes swa;
  311.     XEvent event;
  312.     XColor white;
  313.     int i;
  314.  
  315.     rgb = 1;
  316.     doubleBuf = 1;
  317.     for (i = 1; i < argc; i++) {
  318.         if (argv[i][0] == '-') {
  319.             switch (argv[i][1]) {
  320.               case 'c':
  321.                 rgb = GL_FALSE;
  322.                 break;
  323.           case 's':
  324.         doubleBuf = 0;
  325.         break;
  326.               default:
  327.                 Usage();
  328.             }
  329.         } else {
  330.             Usage();
  331.         }
  332.     }
  333.  
  334.     dpy = XOpenDisplay(0);
  335.     if (!dpy) {
  336.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  337.     return -1;
  338.     }
  339.  
  340.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  341.              doubleBuf ? 
  342.              (rgb ? RGB_attributes_DB : CI_attributes_DB) :
  343.              (rgb ? RGB_attributes : CI_attributes));
  344.     if (!vi) {
  345.     fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
  346.         getenv("DISPLAY"));
  347.     return -1;
  348.     }
  349.  
  350.     printf("Drawing with one context, two windows.\n");
  351.     if (rgb) {
  352.     printf("rgb mode, ");
  353.     } else {
  354.     printf("color index mode, ");
  355.     }
  356.     if (doubleBuf) {
  357.     printf("double buffered\n");
  358.     } else {
  359.     printf("single buffered\n");
  360.     }
  361.  
  362.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  363.                rgb ? AllocNone : AllocAll);
  364.     if (rgb) {
  365.     white.red = ~0;
  366.     white.green = ~0;
  367.     white.blue = ~0;
  368.     XAllocColor(dpy, cmap, &white);
  369.     swa.background_pixel = white.pixel;
  370.     } else {
  371.     swa.background_pixel = 15;
  372.     }
  373.  
  374.     if (!rgb) {
  375.     XColor buf;
  376.     int i;
  377.  
  378.     buf.flags = DoRed | DoGreen | DoBlue;
  379.  
  380.     /* Init color map */
  381.     for (i=0; i<16; i++) {
  382.         buf.pixel = i;
  383.         buf.red = (i & 4) ? 65535 : 0;
  384.         buf.green = (i & 2) ? 65535 : 0;
  385.         buf.blue = (i & 1) ? 65535 : 0;
  386.         XStoreColor(dpy, cmap, &buf);
  387.     }
  388.     }
  389.  
  390.     swa.border_pixel = 0;
  391.     swa.colormap = cmap;
  392.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  393.     | KeyReleaseMask;
  394.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  395.                W, H,
  396.                0, vi->depth, InputOutput, vi->visual,
  397.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  398.                &swa);
  399.     XSetWMColormapWindows(dpy, window, &window, 1);
  400.     XMapWindow(dpy, window);
  401.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  402.  
  403.     window2 = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  404.                W, H,
  405.                0, vi->depth, InputOutput, vi->visual,
  406.                CWBackPixel|CWBorderPixel|CWColormap|CWEventMask,
  407.                &swa);
  408.     XSetWMColormapWindows(dpy, window2, &window2, 1);
  409.     XMapWindow(dpy, window2);
  410.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window2);
  411.  
  412.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  413.     if (!glXMakeCurrent(dpy, window, cx)) {
  414.     fprintf(stderr, "Can't make window current to context\n");
  415.     return -1;
  416.     }
  417.     Init();
  418.  
  419.     for (;;) {
  420.     if (XPending(dpy)) do {
  421.         XNextEvent(dpy, &event);
  422.         switch (event.type) {
  423.           case Expose:
  424.         break;
  425.           case ConfigureNotify:
  426.         W = event.xconfigure.width;
  427.         H = event.xconfigure.height;
  428.         glViewport(0,0,W,H);
  429.         glMatrixMode(GL_PROJECTION);
  430.         glLoadIdentity();
  431.         glOrtho(0,W,0,H,-1,1);
  432.         glMatrixMode(GL_MODELVIEW);
  433.         glClear(GL_COLOR_BUFFER_BIT);
  434.         break;
  435.           case KeyPress:
  436.         {
  437.             char buf[100];
  438.             int rv;
  439.             KeySym ks;
  440.  
  441.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  442.             switch (ks) {
  443.               case XK_F: case XK_f:
  444.               case XK_S: case XK_s:
  445.               case XK_P: case XK_p:
  446.               case XK_L: case XK_l:
  447.               case XK_C: case XK_c:
  448.             break;
  449.               case XK_Escape:
  450.             return 0;
  451.               default:
  452.             break;
  453.             }
  454.         }
  455.         break;
  456.         }
  457.     } while (XPending(dpy) != 0);
  458.  
  459.     Redraw();
  460.     }
  461. }
  462.